package com.weiyi.chat.view.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.weiyi.chat.R;


/**
 * className: HeadLayout
 * description:
 * author: hong
 * datetime: 2016/3/28 0028 上午 9:54
 */
public class HeadLayout extends RelativeLayout {

    private ImageView ivBack;//返回
    private TextView tvTitle;//标题
    private TextView tvRight;//右文本
    private Drawable mLeftImg;
    private String mTitle;
    private String mRightText;
    private int mBgColor;
    private int mTitleColor;
    private int mRightTextColor;

    public HeadLayout(Context context) {
        this(context, null);
    }

    public HeadLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HeadLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.HeadLayout,
                0, 0);

        try {
            mLeftImg = a.getDrawable(R.styleable.HeadLayout_head_left_img);
            mTitle = a.getString(R.styleable.HeadLayout_head_title);
            mRightText = a.getString(R.styleable.HeadLayout_head_right_text);
            mBgColor = a.getInt(R.styleable.HeadLayout_head_bg_color, 0);
            mTitleColor = a.getInt(R.styleable.HeadLayout_head_title_color, 0);
            mRightTextColor = a.getInt(R.styleable.HeadLayout_head_right_text_color, 0);
        } finally {
            a.recycle();
        }

        initView();
    }


    private void initView() {
        View view = View.inflate(getContext(), R.layout.layout_head, this);

        RelativeLayout layout = (RelativeLayout) view.findViewById(R.id.rl_head);
        ivBack = (ImageView) view.findViewById(R.id.iv_back);
        tvTitle = (TextView) view.findViewById(R.id.tv_title);
        tvRight = (TextView) view.findViewById(R.id.tv_right);

        if (mBgColor != 0)
            layout.setBackgroundColor(mBgColor);
        if (mTitleColor != 0)
            tvTitle.setTextColor(mTitleColor);
        if (mRightTextColor != 0)
            tvRight.setTextColor(mRightTextColor);

        setBackImg(mLeftImg);
        setTitle(mTitle);
        setRightText(mRightText);


    }

    /**
     * 设置返回图标
     */
    public void setBackImg(Drawable img) {
        if (mLeftImg != null)
            ivBack.setImageDrawable(img);
    }

    public void setBackImg(Context context, int resId) {
        setBackImg(context.getResources().getDrawable(resId));
    }


    /**
     * 设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        if (title != null)
            tvTitle.setText(title);
    }


    /**
     * 设置右文本
     *
     * @param text
     */
    public void setRightText(String text) {
        if (text != null) {
            tvRight.setVisibility(View.VISIBLE);
            tvRight.setText(text);
        }
    }

    public void setRightTextClick(OnClickListener onClickListener){
        tvRight.setOnClickListener(onClickListener);
    }


    /**
     * 设置返回按钮是否可见
     *
     * @param state
     */
    public void setBackVisibility(boolean state) {
        ivBack.setVisibility(state ? View.VISIBLE : View.INVISIBLE);
    }

    /**
     * 设置右文本是否可见
     *
     * @param state
     */
    public void setRightTextVisibility(boolean state) {
        tvRight.setVisibility(state ? View.VISIBLE : View.INVISIBLE);
    }
}
